<?php

/* 用户会员中心类 */
class service_user extends gdl_components_baseservice
{

	/* 所有错误提示信息 */
	public $error ='';
	public $ob_kqMember_model;
	public $ob_agkUnits_model;
	
 
	
	public function __construct(){
		$this->ob_kqMember_model =  new model_attendance_member();
		$this->ob_agkUnits_model =  new model_lhbase_units();
		
		
	}

	/*
	*@func	getMemberUnitsRelation 获取当前平台的组织架构和考勤用户的关联数组
	*@param	$platformId 平台ID
	*@param	$unitid 组织架构ID
	*@param	$type 返回类型 1 树形组织架构返回 2不树形返回
	*@param	$child 默认子集为_child 可修改
	*@param	$title 模糊查询部门名
	*@result 返回数组
	*/	
	
	public function getMemberUnitsRelation($platformId,$adminUserId = 0,$type=1,$child="_child",$title="",$unitIdR=0){

        //获取该平台的组织架构
        $units = [];
        $MemberInfo = [];
        $globals = new service_globals();
        $aa='lg';
//        if ($unitIdR){
//            $unitid = [$unitIdR];
//        }else{
//
//        }
        $appadminuser = new model_lhbase_appadminuser();
        $unitid = $appadminuser->operating($adminUserId,0,$platformId);
        $UnitsRelation = $this->ob_agkUnits_model->getMenuSpecial($aa,$platformId,$unitid,$title);
        if (!empty($UnitsRelation)){
            //处理数据
            if ($type==1){
                foreach ($UnitsRelation as $key=>$value){
                    $units[$key]['hasChildren'] = $value['hasChildren'];
                    $units[$key]['id'] = $value['id'];
                    $units[$key]['level'] = $value['level'];
                    $units[$key]['pid'] = $value['pid'];
                    $units[$key]['title'] = $value['title'];
                }
                $units = $this->list_to_tree($units,'id','pid',$child,0,$platformId);
            }
            elseif ($type==2){
                //处理数据
                foreach ($UnitsRelation as $key=>$value){
                    $units[] = [
                        'hasChildren'=>$value['hasChildren'],
                        'id'=>$value['id'],
                        'level'=>$value['level'],
                        'pid'=>$value['pid'],
                        'title'=>$value['title'],
                    ];
                }
                foreach ($units as $ke=>$va){
                    $members = $this->ob_kqMember_model->select(" unitId = {$units[$ke]['id']} and platformId=$platformId and status=2")->items;
                    if ($members){
                        foreach ($members as $keys=>$value){
                            $MemberInfo[] = [
                                'userName'=>$value['userName'],
                                'realName'=>$value['realName'],
                                'unitId'=>$value['unitId'],
                                'id'=>$value['id']
                            ];
                        }
                    }
                }
            }
            elseif ($type==3){
                //处理数据
                foreach ($UnitsRelation as $key=>$value){
                    $units[$key]['hasChildren'] = $value['hasChildren'];
                    $units[$key]['id'] = $value['id'];
                    $units[$key]['level'] = $value['level'];
                    $units[$key]['pid'] = $value['pid'];
                    $units[$key]['title'] = $value['title'];
                }
                $units = $this->list_to_tree($units,'id','pid',$child,0,$platformId,2);
            }
        }

//		$result = serialize($UnitsRelation);
		return ['relation'=>$units,'members'=>$MemberInfo];
	}

    /**
     * 节点遍历
     *
     * @param        $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int    $root
     *
     * @return array
     */
    private function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0,$platformId=0,$tt=1) {
        // 创建Tree
        $tree = [];
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                if ($data instanceof \think\Model) {
                    $list[$key] = $data->toArray();
                }
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                if (!isset($list[$key][$child])) {
                    $list[$key][$child] = [];
                }
                $parentId = $data[$pid];
                if ($tt==1){
                    $member = [];
                    $members = $this->ob_kqMember_model->select(" unitId = {$list[$key]['id']} and platformId=$platformId and status=2")->items;
                    if ($members){
                        foreach ($members as $keys=>$value){
                            $member[] = [
                                'userName'=>$value['userName'],
                                'realName'=>$value['realName'],
                                'unitId'=>$value['unitId'],
                                'id'=>$value['id']
                            ];
                        }
                    }
                    $list[$key]['member'] = $member;
                }elseif ($tt==2){
                    $members = $this->ob_kqMember_model->select(" unitId = {$list[$key]['id']} and platformId=$platformId and status=2")->items;
                    if ($members){
                        foreach ($members as $keys=>$value){
                            $member = [
                                'userName'=>$value['userName'],
                                'realName'=>$value['realName'],
                                'unitId'=>$value['unitId'],
                                'id'=>$value['id']
                            ];
                            array_push($list[$key][$child],$member);
                        }
                    }
                }
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        if ($tt==2){
                            $parent = & $refer[$parentId];
                            $parent[$child][] = & $list[$key];
                            $members = $this->ob_kqMember_model->select(" unitId = {$parentId} and platformId=$platformId and status=2")->items;
                            if ($members){
                                foreach ($members as $keys=>$value){
                                    $member = array(
                                        'userName'=>$value['userName'],
                                        'realName'=>$value['realName'],
                                        'unitId'=>$value['unitId'],
                                        'id'=>$value['id']
                                    );
                                    array_push($parent[$child],$member);
                                }
                            }
                        }else{
                            $parent = & $refer[$parentId];
                            $parent[$child][] = & $list[$key];
                        }
                    }
                }
            }
        }

        return $tree;
    }

    /*
        *@func	getUnitsRelation 获取当前平台集合下的所有平台ID
        *@param	$unitids 组织架构IDs
        *@result 返回数组
        */

    public function getUnitsRelation($units,$unitids, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0){

        //获取该平台的组织架构 $units
        // 创建Tree
        $tree = [];


        return $tree;
    }


    /**
     * 资源检索 例如审批流程
     * @param int $unitId
     * @param int $platformId
     * @param int $adminUserId
     * @return bool
     */
    public function updateAttBaseResource($unitId=0,$platformId=0,$adminUserId=0){
        if ($platformId==0)return false;
        if ($adminUserId==0)return false;
        //检索考勤默认审批流程资源是否存在 start
        $approvalprocess = new model_attendance_approvalprocess();
        $approvalprocessList = $approvalprocess->select("status=1 and platformId = {$platformId} and adminUserId = {$adminUserId}")->items;
        if ($approvalprocessList){
            $approvalprocessTypes = array_column($approvalprocessList,'type');
        }else{
            $approvalprocessTypes = [0];
        }
        if (!in_array(1,$approvalprocessTypes)){//1请假流程 2外出流程 3补卡流程 4特殊流程
            $approvalprocess->insert([
                'status'=>1,
                'type'=>1,
                'stop'=>1,
                'rules'=>"a:1:{i:0;a:2:{s:5:\"level\";s:1:\"1\";s:4:\"type\";s:17:\"target_management\";}}",
                'name'=>"默认请假审批流程",
                'createTime'=>time(),
                'platformId'=>$platformId,
                'adminUserId'=>$adminUserId,
                ]
            );
        }
        if (!in_array(2,$approvalprocessTypes)){
            $approvalprocess->insert([
                    'status'=>1,
                    'type'=>2,
                    'stop'=>1,
                    'rules'=>"a:1:{i:0;a:2:{s:5:\"level\";s:1:\"1\";s:4:\"type\";s:17:\"target_management\";}}",
                    'name'=>"默认外出审批流程",
                    'createTime'=>time(),
                    'platformId'=>$platformId,
                    'adminUserId'=>$adminUserId,
                ]
            );
        }
        if (!in_array(3,$approvalprocessTypes)){
            $approvalprocess->insert([
                    'status'=>1,
                    'type'=>3,
                    'stop'=>1,
                    'rules'=>"a:1:{i:0;a:2:{s:5:\"level\";s:1:\"1\";s:4:\"type\";s:17:\"target_management\";}}",
                    'name'=>"默认补卡审批流程",
                    'createTime'=>time(),
                    'platformId'=>$platformId,
                    'adminUserId'=>$adminUserId,
                ]
            );
        }
        //检索考勤默认审批流程资源是否存在 end
		
		#查询该平台是否有默认分组
		$positionGroupModel = new model_attendance_positiongroup();
		$emptyGroupDefault = $positionGroupModel->selectOne("platformId={$platformId} and isDefault=1 and isDelete=0");
		#没有默认分组就注册一个
		if(empty($emptyGroupDefault)){
			$insertGroup = ['title'=>'默认分组', 'sort'=>9999999, 'pid'=>0,'createTime'=>time(),'platformId'=>$platformId, 'isDelete'=>0,'isDefault'=>1];
			$positionGroupModel->insert($insertGroup);
		}
        return true;
    }


    /**
     * 往上找级别
     * @param $unit
     * @return array
     */
    public function getUpLeaveUnitId($unit){
        if (!$unit)return [];
        static $data = [];
        $unitsModel =  new model_lhbase_units();
        $unitInfo = $unitsModel->selectOne(" id = {$unit} and status=1 and isDelete=1","id,level,parentId pid");
        if ($unitInfo){
            if ($unitInfo['level']==0){
                ob_clean();
                return $data;
            }else{
                $data[] =  & $unitInfo;
                $this->getUpLeaveUnitId($unitInfo['pid']);
            }
        }
        ob_clean();
        return $data;
    }
}	
